############################################################
# Who is anxious?
# 
# Author: 
# Soubhik Barari
# 
# Environment:
# - must use R 3.6
# 
# Input:
# - 00-read_clean_data.R
#
# Output:
# - fig/anxiety{.png,.pdf}
############################################################

source("00-read_clean_data.R")

#----------------------------------------------------------
# summarise overall response
#----------------------------------------------------------

m <- mean(responses_df$anxiety_index, na.rm=T)
responses_df %>%
ggplot() + 
	geom_density(aes(x=anxiety_index), fill="gray32") +
	xlab(sprintf("anxiety index (n=%d)", nrow(responses_df))) + ylab("density") +
    geom_segment(x=m, xend=m, y=0, yend=0.025, lty=2) +
    annotate("text", label=sprintf("%0.1f", m), x=m-4, y=0.026, size=4) +
    xlim(c(0, 100)) +
	theme_bw()
ggsave("fig/anxiety_scale.png", width=5, height=2.5)
ggsave("fig/anxiety_scale.pdf", width=5, height=2.5)

#----------------------------------------------------------
# overall response x group
#----------------------------------------------------------

library(scales)
responses_df %>% 
    filter(gender != "Other") %>%
    tidyr::gather("variable", "variable_value", c("gender", "health", "age_group")) %>%
    select(variable, variable_value, anxiety_index) %>%
    mutate(variable = 
               ifelse(variable == "age_group", "age group",
                      ifelse(variable == "health", "general health",
                             ifelse(variable == "past_healthy_behavior", "past healthy\nbehavior", "gender"
                             )))) %>%
    mutate(variable_value = factor(variable_value, levels=c(
        "18-29", "30-39", "40-49", "60+", "Poor", "Fair", "Good", "Excellent", "Male", "Female", "Other"
    ))) %>%
    group_by(variable, variable_value) %>%
    summarise(
        y = mean(anxiety_index, na.rm=T),
        ymin = mean(anxiety_index, na.rm=T)-(1.96*sd(anxiety_index, na.rm=T)/sqrt(n())),
        ymax = mean(anxiety_index, na.rm=T)+(1.96*sd(anxiety_index, na.rm=T)/sqrt(n()))
    ) %>%
    filter(!is.na(variable_value) & variable_value != "") %>%
    ggplot(aes(x=variable_value, y=y)) +
    geom_text(aes(label=sprintf("%0.1f", y), x=variable_value, y=ymax+2), size=3) +
    geom_bar(stat="identity", position = position_dodge(width=0.9)) +
    geom_errorbar(aes(ymin=ymin, ymax=ymax), position = position_dodge(width=0.9), width = 0, size=1, alpha=0.8) +
    coord_flip() +
    scale_y_continuous(limits=c(50, 82),oob = rescale_none) +
    facet_grid(variable ~ ., scales = "free", space = "free") +
    xlab("") + ylab(sprintf("average anxiety index (n=%d)", nrow(responses_df))) +
    theme_bw()
ggsave("fig/anxiety_scale_x_group.png", width=5, height=3)
ggsave("fig/anxiety_scale_x_group.pdf", width=5, height=3)

#----------------------------------------------------------
# summarise response by question
#----------------------------------------------------------

#TODO
#----------------------------------------------------------
# individual question responses x group
#----------------------------------------------------------

#TODO
